package com.example.forum_system.controller;

import com.example.forum_system.common.AppResult;
import com.example.forum_system.common.ResultCode;
import com.example.forum_system.config.AppConfig;
import com.example.forum_system.model.Article;
import com.example.forum_system.model.ArticleReply;
import com.example.forum_system.model.User;
import com.example.forum_system.service.IArticleReplyService;
import com.example.forum_system.service.IArticleService;
import lombok.NonNull;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Li_yizYa
 * Date: 2025/4/29
 * Time: 13:08
 */
@Slf4j
@Api(tags = "回复接口")
@RestController
@RequestMapping("/reply")
public class ArticleReplyController {

    @Resource
    private IArticleService articleService;
    @Resource
    private IArticleReplyService articleReplyService;

    @ApiOperation("回复帖子")
    @PostMapping("/create")
    public AppResult create(HttpServletRequest request,
                            @ApiParam("帖子Id") @RequestParam("articleId") @NonNull Long articleId,
                            @ApiParam("帖子内容") @RequestParam("content") @NonNull String content) {
        // 获取用户
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        // 判断用户是否已禁言
        if (user.getState() == 1) {
            // 表示已禁言
            return AppResult.failed(ResultCode.FAILED_USER_BANNED);
        }
        // 获取要回复的帖子对象
        Article article = articleService.selectById(articleId);
        // 是否存在，或已删除
        if (article == null || article.getDeleteState() == 1) {
            // 表示已删除或不存在
            return AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS);
        }
        // 是否封贴
        if (article.getState() == 1) {
            // 表示已封贴
            return AppResult.failed(ResultCode.FAILED_ARTICLE_BANNED);
        }
        // 构建回复对象
        ArticleReply articleReply = new ArticleReply();
        articleReply.setArticleId(articleId); // 主帖id
        articleReply.setPostUserId(article.getUserId());// 回复的发送者
        articleReply.setContent(content); // 回复的内容

        // 写入回复
        articleReplyService.create(articleReply);
        // 返回结果
        return AppResult.success();
    }

    @ApiOperation("获取帖子回复列表")
    @GetMapping("/getReplies")
    public AppResult<List<ArticleReply>> getRepliesByArticleId(@ApiParam("帖子Id") @RequestParam("articleId") @NonNull Long articleId) {
        // 校验帖子是否存在
        Article article = articleService.selectById(articleId);
        if (article == null || article.getDeleteState() == 1) {
            // 返回错误提示
            return AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS);
        }
        // 调用 Service 获取结果
        List<ArticleReply> ret = articleReplyService.selectByArticleId(articleId);
        // 返回结果
        return AppResult.success(ret);
    }
}
